package com.vichat.common.util;

import com.enterprisedt.net.ftp.*;
import com.vichat.common.vo.ftp.Ftp;

import java.io.IOException;

public class FtpUtil {
	public static void main(String[] args) {
		Ftp ftp = new Ftp();
		ftp.setIp("127.0.0.1");
		ftp.setLocalPath("c:\\test\\");
		ftp.setUsername("mp4user");
		ftp.setPassword("mp4user");
		ftp.setRemotePath("/ftp");
		FtpUtil.testFtp(ftp);
	}

	public static boolean testFtp(Ftp ftp) {
		boolean flag = true;
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.setRemoteHost(ftp.getIp());
			FTPMessageCollector listener = new FTPMessageCollector();
			ftpClient.setMessageListener(listener);
			ftpClient.setControlEncoding(System.getProperty("file.encoding"));
			ftpClient.connect();
			ftpClient.login(ftp.getUsername(), ftp.getPassword());
			ftpClient.setConnectMode(FTPConnectMode.PASV);
			ftpClient.setType(FTPTransferType.BINARY);
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ftpClient.quit();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (FTPException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	public static boolean getFile(Ftp ftp, String fileName) {
		boolean flag = true;
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.setRemoteHost(ftp.getIp());
			FTPMessageCollector listener = new FTPMessageCollector();
			ftpClient.setMessageListener(listener);
			ftpClient.setControlEncoding(System.getProperty("file.encoding"));
			ftpClient.connect();
			ftpClient.login(ftp.getUsername(), ftp.getPassword());
			ftpClient.setConnectMode(FTPConnectMode.PASV);
			ftpClient.setType(FTPTransferType.BINARY);
			if (fileName == null) {
				ftpClient.chdir(ftp.getRemotePath());
				String[] fileList = ftpClient.dir();
				for (String name : fileList) {
					System.out.println("Begin download " + name);
					ftpClient.get(ftp.getLocalPath() + name, name);
					System.out.println("End download " + name);
				}
			} else {
				ftpClient.get(ftp.getLocalPath(), ftp.getRemotePath() + "/" + fileName);
			}
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ftpClient.quit();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (FTPException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	public static boolean uploadFile(Ftp ftp, String localPath, String remoteFile) {
		boolean flag = true;
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.setRemoteHost(ftp.getIp());
			FTPMessageCollector listener = new FTPMessageCollector();
			ftpClient.setMessageListener(listener);
			ftpClient.setControlEncoding(System.getProperty("file.encoding"));
			ftpClient.connect();
			ftpClient.login(ftp.getUsername(), ftp.getPassword());
			ftpClient.setConnectMode(FTPConnectMode.PASV);
			ftpClient.setType(FTPTransferType.BINARY);

			System.out.println("Begin put " + remoteFile);
			ftpClient.put(localPath, remoteFile);
			System.out.println("End put " + remoteFile);

		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ftpClient.quit();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (FTPException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	public static boolean deleteFile(Ftp ftp, String remoteFile) {
		boolean flag = true;
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.setRemoteHost(ftp.getIp());
			FTPMessageCollector listener = new FTPMessageCollector();
			ftpClient.setMessageListener(listener);
			ftpClient.setControlEncoding(System.getProperty("file.encoding"));
			ftpClient.connect();
			ftpClient.login(ftp.getUsername(), ftp.getPassword());
			ftpClient.setConnectMode(FTPConnectMode.PASV);
			ftpClient.setType(FTPTransferType.BINARY);

			System.out.println("Begin delete " + remoteFile);
			ftpClient.delete(remoteFile);
			System.out.println("End delete " + remoteFile);

		} catch(FTPException e){
			if (e.getMessage().indexOf("File not found") >= 0) {
				System.out.println("服务器上无该文件:"+remoteFile);
			}else{
				flag = false;
				e.printStackTrace();
			}
		}catch (Exception e) {
			flag = false;
			e.printStackTrace();
		} finally {
			try {
				ftpClient.quit();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (FTPException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
}
